/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.cjb.malacma.rssfeed.googleapi;

import com.google.gdata.client.GoogleService;
import com.google.gdata.client.blogger.BloggerService;
import com.google.gdata.data.Entry;
import com.google.gdata.data.Feed;
import com.google.gdata.data.Person;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.cjb.malacma.rssfeed.entity.FeedUrl;
import net.cjb.malacma.rssfeed.util.FeedFacade;
import net.cjb.malacma.rssfeed.util.FeedFactory;

/**
 *
 * @author malacma
 */
public class BloggerClient {

    private static BloggerService myService = new BloggerService(java.util.UUID.randomUUID().toString());
    private static final String METAFEED_URL = "http://www.blogger.com/feeds/default/blogs";
    private static final String FEED_URI_BASE = "http://www.blogger.com/feeds";
    private static final String POSTS_FEED_URI_SUFFIX = "/posts/default";
    private static final String COMMENTS_FEED_URI_SUFFIX = "/comments/default";
    private static String feedUri = FEED_URI_BASE + "rss-feed-crawler";

    public static StringBuilder getBlogListHtmlMarquee(String user, String pass) throws AuthenticationException, MalformedURLException, IOException, ServiceException {
        myService.setUserCredentials(user, pass);
        // Request the feed
        final URL feedUrl = new URL(METAFEED_URL);
        Feed resultFeed = myService.getFeed(feedUrl, Feed.class);
        StringBuilder sb = new StringBuilder();
        // Print the results
        System.out.println(resultFeed.getTitle().getPlainText());
        for (int i = 0; i < resultFeed.getEntries().size(); i++) {
            Entry entry = resultFeed.getEntries().get(i);
            sb.append("<a href='");
            sb.append(entry.getHtmlLink().getHref());
            sb.append("' target='_BLANK'>");
            sb.append(entry.getTitle().getPlainText());
            sb.append("</a> - ");
        }
        return sb;
    }

    public static void createPost(String userName, String pass) throws ServiceException, IOException {
        // Create the entry to insert

        myService.setUserCredentials(userName, pass);

        List<FeedUrl> feeds = FeedFacade.findAllOnffLine();
        String blogId = getBlogId(myService);
        feedUri = FEED_URI_BASE + "/" + blogId;
        URL postUrl = new URL(feedUri + POSTS_FEED_URI_SUFFIX);


        for (FeedUrl f1 : feeds) {
            try {
                StringBuilder sb = new StringBuilder();
                sb = FeedFacade.makeLayout(f1, sb);


                if (sb == null) {
                    continue;
                }

                Entry myEntry = new Entry();
                myEntry.setTitle(new PlainTextConstruct(f1.getTitulo()));
                myEntry.setContent(new PlainTextConstruct(sb.toString()));
                Person author = new Person("Z-BOT-U", null, "malacma");
                myEntry.getAuthors().add(author);
                myEntry.setDraft(false);
                myService.insert(postUrl, myEntry);

                FeedFacade.updateFeed(f1);
            } catch (MalformedURLException ex) {
                Logger.getLogger(BloggerClient.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(BloggerClient.class.getName()).log(Level.SEVERE, null, ex);
            } catch (FeedException ex) {
                Logger.getLogger(BloggerClient.class.getName()).log(Level.SEVERE, null, ex);
            }


        }
    // Ask the service to insert the new entry


    }

    public static void main(String[] args) {
        try {
            createPost("pino.rss.feed", "D4onde12345");
        } catch (ServiceException ex) {
            Logger.getLogger(BloggerClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(BloggerClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static String getBlogId(BloggerService myService)
            throws ServiceException, IOException {
        // Get the metafeed
        final URL feedUrl = new URL(METAFEED_URL);
        Feed resultFeed = myService.getFeed(feedUrl, Feed.class);

        // If the user has a blog then return the id (which comes after 'blog-')
        if (resultFeed.getEntries().size() > 0) {
            Entry entry = resultFeed.getEntries().get(0);
            return entry.getId().split("blog-")[1];
        }
        throw new IOException("User has no blogs!");
    }
}
